package com.bishe.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bishe.dao.ResourceInfoDao;
import com.bishe.entity.PersonnelInfo;
import com.bishe.entity.ResourceInfo;
import com.bishe.entity.query.ResourceQuery;
import com.bishe.service.PersonnelInfoService;
import com.bishe.service.ResourceInfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * (ResourceInfo)表服务实现类
 *
 * @author makejava
 * @since 2024-03-14 18:40:27
 */
@Service("resourceInfoService")
public class ResourceInfoServiceImpl extends ServiceImpl<ResourceInfoDao, ResourceInfo> implements ResourceInfoService {

    @Resource
    private PersonnelInfoService personnelInfoService;

    @Override
    public IPage<ResourceInfo> getPage(ResourceQuery query) {

        // 条件为空 默认查询
        if (StrUtil.isAllEmpty(query.getIdCard(), query.getName(), query.getPhone())) {
            IPage<ResourceInfo> page = page(query.toPage(), Wrappers.emptyWrapper());
            for (ResourceInfo record : page.getRecords()) {
                PersonnelInfo byId = personnelInfoService.getById(record.getUid());
                record.setIdCard(byId.getIdCard());
                record.setName(byId.getName());
                record.setPhone(byId.getPhone());
                record.setSex(byId.getSex());
                record.setEducation(byId.getEdBackground());
            }
            return page;
        }

        List<PersonnelInfo> personnelInfoList = personnelInfoService.list(Wrappers.lambdaQuery(PersonnelInfo.class)
                .like(StrUtil.isNotBlank(query.getIdCard()), PersonnelInfo::getIdCard, query.getIdCard())
                .like(StrUtil.isNotBlank(query.getName()), PersonnelInfo::getName, query.getName())
                .like(StrUtil.isNotBlank(query.getPhone()), PersonnelInfo::getPhone, query.getPhone())
        );

        Map<String, List<PersonnelInfo>> perCollect = new HashMap<>();
        if (CollUtil.isNotEmpty(personnelInfoList)) {
            perCollect = personnelInfoList.stream().collect(Collectors.groupingBy(PersonnelInfo::getId));
        }

        IPage<ResourceInfo> resultPage = new Page<>();

        IPage<ResourceInfo> page = page(query.toPage(), Wrappers.emptyWrapper());

        List<ResourceInfo> resourceInfos = new ArrayList<>();

        for (ResourceInfo record : page.getRecords()) {
            String uid = record.getUid();
            if (perCollect.containsKey(uid)) {
                PersonnelInfo personnelInfo = perCollect.get(uid).get(0);
                record.setIdCard(personnelInfo.getIdCard());
                record.setName(personnelInfo.getName());
                record.setPhone(personnelInfo.getPhone());
                record.setSex(personnelInfo.getSex());
                record.setEducation(personnelInfo.getEdBackground());
                resourceInfos.add(record);
            }
        }
        resultPage.setRecords(resourceInfos);
        resultPage.setCurrent(page.getCurrent());
        resultPage.setSize(page.getSize());
        resultPage.setTotal(page.getTotal());
        return resultPage;
    }

}

